package com.wspyydshhh.mytool.util;

import ohos.app.Context;
import ohos.data.distributed.common.*;
import ohos.data.distributed.user.SingleKvStore;

import java.util.ArrayList;

public class DBUtil {
    /**
     * 初始化数据库
     * @param context 上下文
     * @param storeId 数据库id
     * @return 数据库
     */
    public static SingleKvStore initOrGetDB(Context context,String storeId){
        //id
        FieldNode fdid = new FieldNode("id");
        fdid.setNullable(false);
        fdid.setType(FieldValueType.STRING);
        //标题
        FieldNode fdtitle = new FieldNode("title");
        fdtitle.setType(FieldValueType.STRING);
        //年
        FieldNode fdyear = new FieldNode("year");
        fdyear.setType(FieldValueType.INTEGER);
        //月
        FieldNode fdmonth = new FieldNode("month");
        fdmonth.setType(FieldValueType.INTEGER);
        //日
        FieldNode fdday = new FieldNode("day");
        fdday.setType(FieldValueType.INTEGER);
        //时间戳
        FieldNode fdtimes = new FieldNode("times");
        fdtimes.setType(FieldValueType.LONG);
        //数值
        FieldNode fdoperationNum = new FieldNode("operationNum");
        fdoperationNum.setType(FieldValueType.DOUBLE);
        //备注
        FieldNode fdremarks = new FieldNode("remarks");
        fdremarks.setType(FieldValueType.STRING);
        //操作人
        FieldNode fdoperationName = new FieldNode("operationName");
        fdoperationName.setType(FieldValueType.STRING);
        //是否领取
        FieldNode fdhasTask = new FieldNode("hasTask");
        fdhasTask.setType(FieldValueType.INTEGER);
        //是否为加
        FieldNode fdisAdd = new FieldNode("isAdd");
        fdisAdd.setType(FieldValueType.BOOLEAN);

        //把上面的字段，封装到Schema对象
        Schema schema = new Schema();
        ArrayList<String> indexList = new ArrayList<>();
        indexList.add("$.id");
        schema.setIndexes(indexList);
        schema.getRootFieldNode().appendChild(fdid);
        schema.getRootFieldNode().appendChild(fdtitle);
        schema.getRootFieldNode().appendChild(fdyear);
        schema.getRootFieldNode().appendChild(fdmonth);
        schema.getRootFieldNode().appendChild(fdday);
        schema.getRootFieldNode().appendChild(fdtimes);
        schema.getRootFieldNode().appendChild(fdoperationNum);
        schema.getRootFieldNode().appendChild(fdremarks);
        schema.getRootFieldNode().appendChild(fdoperationName);
        schema.getRootFieldNode().appendChild(fdhasTask);
        schema.getRootFieldNode().appendChild(fdisAdd);
        schema.setSchemaMode(SchemaMode.STRICT);

        KvManagerConfig kvManagerConfig = new KvManagerConfig(context);
        KvManager kvManager = KvManagerFactory.getInstance().createKvManager(kvManagerConfig);
        Options options = new Options();
        options.setCreateIfMissing(true)
                .setEncrypt(false)
                .setKvStoreType(KvStoreType.SINGLE_VERSION)
                .setSchema(schema);
        return kvManager.getKvStore(options, storeId);
    }
}
